home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c
- Subject: Re: Problem...Problem!
- Date: 1 Feb 96 00:53:10 GMT
- Organization: Clemson University
- Message-ID: <mjs.823135990@hubcap>
- References: <4el09q$6im@ratree.psu.ac.th> <310FB217.280C@microsports.com>
- NNTP-Posting-Host: hubcap.clemson.edu
-
- "Superstition ain't the way!"
-
- ---Stevie Wonder
-
- "Eric W. Bradway" <ebradway@microsports.com> thoughtfully analyzes
- a program posted by Sanon CHAOCHAIYAPORN:
-
- >> #define NUM 8
- >> #define ERR 1e-4
-
- >Try avoiding the scientific notation by doing a divide by 10000 and
- >comparing to 1. I assume this isn't a time-critical routine so the extra
- >divide won't hurt. If it is time-critical, rewrite the program using
- >long ints.
-
- What's your reasoning here? What's wrong with a perfectly good double
- constant that is improved by doing the division, and what on earth is
- the point of using long ints?
-
- >> v[i] = (v[i+1] + v[i+2]) / 8;
- >> v[i+1] = (v[i] + v[i+3] + 40) / 8;
- >> v[i+2] = (v[i] + v[i+3] + v[i+5] + 50) / 8;
- >> v[i+3] = (v[i+1] + v[i+2] + 110) / 8;
- >> v[i+4] = (v[i+5] + v[i+6] + 150) / 8;
- >> v[i+5] = (v[i+2] + v[i+4] + v[i+7] + 70) / 8;
- >> v[i+6] = (v[i+4] + v[i+7] + 200) / 8;
- >> v[i+7] = (v[i+5] + v[i+6] + 200) / 8;
-
- >Your problem may lie here - add a .0 to each numeric constant (e.g.,
- >8.0, 40.0, 110.0) to get the compiler to treat them as floats. Many
- >compilers automatically convert everything to ints if you don't
- >specifically show that the values/variables are floats.
-
- Since v[] is an array of floats, all these computations will be done
- as floats. Every (working) compiler will do this correctly. Every
- (working) compiler will do the arithmetic with integral types if and
- only if all the operands are integral types.
-
- >And of course, on closer inspection, when do you assign meaningful
- >values to v[]? Do you mean to initialize to zero and get the following
- >results on the first pass:
-
- He does mean this, and he says so in a loop before the main loop starts.
-
- >[...]
-
- Did you think about the changes you suggest, and what effect they
- might have? Did you even try them out before posting?
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-